//创建路由规则
import { createBrowserRouter } from "react-router-dom";

import MainLayout from "../layouts/MainLayout";
import ManageLayout from "../layouts/ManageLayout";
import QuestionLayout from "../layouts/QuestionLayout";

import Home from "../pages/Home";
import Login from "../pages/Login";
import Register from "../pages/Register";

import Questionlist from "../pages/Questionlist";
import QuestionStart from "../pages/QuestionStart";
import QuestionTrash from "../pages/QuestionTrash";

// import QuestionEdit from "../pages/QuestionEdit";
// import QuestionStat from "../pages/QuestionStat";

//使用路由懒加载 拆分bundle 优化首页体积
const QuestionEdit = lazy(() => import(/*webpackChunkName: 'QuestionEdit'*/ "../pages/QuestionEdit"));
const QuestionStat = lazy(() => import(/*webpackChunkName: 'QuestionStat'*/ "../pages/QuestionStat"));

import NotFund from "../pages/NotFund";
import { lazy } from "react";

const BrowserRouter = createBrowserRouter([
  {
    path: "/",
    Component: MainLayout,
    children: [
      {
        path: "/",
        Component: Home,
      },
      {
        path: "/login",
        Component: Login,
      },
      {
        path: "/register",
        Component: Register,
      },
      {
        path: "/manage",
        Component: ManageLayout,
        children: [
          {
            path: "list",
            Component: Questionlist,
          },
          {
            path: "start",
            Component: QuestionStart,
          },
          {
            path: "trash",
            Component: QuestionTrash,
          },
        ],
      },
      {
        path: "*",
        Component: NotFund,
      },
    ],
  },
  {
    path: "/question",
    Component: QuestionLayout,
    children: [
      {
        path: "edit/:id",
        Component: QuestionEdit,
      },
      {
        path: "stat/:id",
        Component: QuestionStat,
      },
    ],
  },
]);

export default BrowserRouter;

//常用的路由变量
export const HOME_PATHNAME = "/";
export const LOGIN_PATHNAME = "/login";
export const REGISTER_PATHNAME = "/register";
export const MANAGE_INDEX_PATHNAME = "/manage/list";

//判断当时是否是登录页或者注册页
export function isLoginORRegister(pathname: string) {
  if ([LOGIN_PATHNAME, REGISTER_PATHNAME].includes(pathname)) {
    return true;
  } else {
    return false;
  }
}

//判断当前页面是否需要登录

export function isNoNeedUSerInfo(pathname: string) {
  if ([HOME_PATHNAME, LOGIN_PATHNAME, REGISTER_PATHNAME].includes(pathname)) {
    return true;
  } else {
    return false;
  }
}
